import 'dart:async';

import 'package:flutter/material.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/distribution_entity.dart';
import 'package:library_base/event/event.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:library_base/base/base_list_state.dart';
import 'package:module_dispatcher/page/progress/progress_controller.dart';
import 'package:module_dispatcher/page/progress/progress_page_ui.dart';
import 'package:library_base/entities/event/progress_refresh_event.dart';
import 'package:library_base/entities/event/progress_filter_event.dart';

class ProgressPage extends StatefulWidget {
  const ProgressPage({Key? key}) : super(key: key);

  @override
  _ProgressPageState createState() => _ProgressPageState();
}

class _ProgressPageState extends BaseListState<DistributionEntity, DistributionEntity, ProgressPage,
    ProgressController> {

  late StreamSubscription<ProgressRefreshEvent> streamSubscription;

  late StreamSubscription<ProgressFilterEvent> streamRefreshSubscription;

  @override
  ProgressController registerController() => Get.put(ProgressController());

  @override
  void onCreate() {
    super.onCreate();
    setNavigationBarVisible(false);
    setStatusBarVisible(false);
    streamSubscription = Event.eventBus.on<ProgressRefreshEvent>().listen((event) {
      baseGetxController!.reset();
    });
    streamRefreshSubscription = Event.eventBus.on<ProgressFilterEvent>().listen((event) {
      baseGetxController!.stationTimeBegin = event.stationTimeBegin;
      baseGetxController!.tryReLoad();
    });

  }

  @override
  void controllerOnCreate() {
    useRefresh = true;
    isCanRefresh = false;
    isCanLoad = false;
    enableControlFinishLoad = false;
    enableControlFinishRefresh = true;
    baseGetxController?.easyRefreshController = controller;
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    return MultiStateWidget(
      emptyWidget: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            LocalImage('base_empty', package: Constant.packLib(), width: 0.6.sw),
            Text(MultiLanguage.noSuchProduct.tr, style: TextStyles.textSecondary28),
            SizedBox(height: 80.w),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.reload.tr,
                onPressed: baseGetxController!.tryReLoad,
              ),
            ])
          ]),
      controller: baseGetxController!.multiControllerOut,
      child: Column(
        children: [
          Expanded(
            child: getListView(context, baseGetxController),
          ),
        ],
      ),
    );
  }

  @override
  List<Widget> buildListItem(BuildContext context) {
    return baseGetxController!.listData.value.buildItem((index, element) {
      return GetBuilder<ProgressController>(
        builder: (logic) {
          return GestureDetector(
              onTap: () {},
              child: buildItem(
                element,
                index,
                onExpand: _onExpand,
                withdraw: baseGetxController!.withdraw,
                toOrderDetail:baseGetxController!.onTapChild,
              ));
        },
      );
    }, dividingLine: () {
      return line2;
    });
  }

  _onExpand() {
    baseGetxController!.update();
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    super.dispose();
    streamSubscription.cancel();
    streamRefreshSubscription.cancel();
  }

}
